package ltesim.sdkManager.sdklist;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.core.resources.ResourcesPlugin;

/**
 * Container class for SDKList
 * 
 * @author xirka
 */
public class SDKList {

	private ArrayList<String> versionList;
	private ArrayList<String> dirList;
	private BufferedWriter writer;
	private String workspaceDir;

	/**
	 * Location of the ltesim.plugin configuration files relative to eclipse
	 * workspace root
	 */
	private static final String CONFIGPATH = "\\.metadata\\ltesim\\";

	/**
	 * Name of ltesim.plugin.sdkmanager configuration file
	 */
	private static final String CONFIGFILE = "ltesim.sdkmanager.conf";

	/**
	 * Constructor of sdkList<br/>
	 * Initialize automatically by reading the configuration file
	 */
	public SDKList() {
		workspaceDir = ResourcesPlugin.getWorkspace().getRoot().getLocation()
				.toString();
		versionList = new ArrayList<String>();
		dirList = new ArrayList<String>();
		readConfig();
	}

	/**
	 * Read and parse the ltesim.plugin.sdkmanager
	 */
	private void readConfig() {
		File file = new File(workspaceDir + CONFIGPATH + CONFIGFILE);

		if (file.exists()) {

			BufferedReader buf;
			try {
				buf = new BufferedReader(new FileReader(file));
				while (buf.ready()) {
					String temp = buf.readLine();
					if (temp.equals("###")) {
						break;
					}
					versionList.add(temp);
				}
				while (buf.ready()) {
					String temp = buf.readLine();
					dirList.add(temp);
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} else {
			try {
				File dir = new File(workspaceDir + CONFIGPATH);
				dir.mkdirs();
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Rewrite the configuration file
	 */
	public void writeConfig() {
		String temp1 = "", temp2 = "";
		for (int i = 0; i < versionList.size(); ++i) {
			temp1 += versionList.get(i);
			temp1 += "\n";
			temp2 += dirList.get(i);
			temp2 += "\n";
		}
		String temp3 = temp1 + "###\n" + temp2;
		// System.out.println(temp3);

		File file = new File(workspaceDir + CONFIGPATH + CONFIGFILE);
		try {
			writer = new BufferedWriter(new FileWriter(file));
			writer.write(temp3);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * Add a new SDK version paired with it's directory
	 * 
	 * @param version
	 * @param dir
	 */
	public void addVersion(String version, String dir) {
		versionList.add(version);
		dirList.add(dir);
	}

	/**
	 * Remove a single new SDK version paired with it's directory
	 * 
	 * @param version
	 * @param dir
	 */
	public void removeVersion(String version, String dir) {
		versionList.remove(version);
		dirList.remove(dir);
	}

	/**
	 * Remove a SDK on index idx
	 * 
	 * @param idx
	 */
	public void removeVersion(int idx) {
		versionList.remove(idx);
		dirList.remove(idx);
	}

	/**
	 * Get all registered SDK version
	 * 
	 * @return ArrayList<String> of registered SDK Version(s)
	 */
	public ArrayList<String> getVersionList() {
		return versionList;
	}

	/**
	 * Get all registered SDK directory
	 * 
	 * @return ArrayList<String> of registered SDK Directory(s)
	 */
	public ArrayList<String> getDirList() {
		return dirList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String retval = "";
		for (int i = 0; i < versionList.size(); ++i) {
			retval += versionList.get(i);
			retval += " :  ";
			retval += dirList.get(i);
			retval += "\n";
		}
		return retval;
	}

}
